package com.qf.smsplatform.service.Impl;
/*2021/10/31*/

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.cache.TSearchParmasCache;
import com.qf.smsplatform.constans.ResultCode;
import com.qf.smsplatform.events.TSearchParmasEvent;
import com.qf.smsplatform.exception.AddDataException;
import com.qf.smsplatform.exception.QueryDataException;
import com.qf.smsplatform.mapper.TSearchParmasMapper;
import com.qf.smsplatform.pojo.CheckType;
import com.qf.smsplatform.pojo.TSearchParmas;
import com.qf.smsplatform.service.TSearchParmasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @YuTao
 */
@Service
@Transactional
public class TSearchParmasServiceImpl implements TSearchParmasService {

    private TSearchParmasMapper tSearchParmasMapper;
    @Autowired
    public void settSearchParmasMapper(TSearchParmasMapper tSearchParmasMapper) {
        this.tSearchParmasMapper = tSearchParmasMapper;
    }

    private TSearchParmasCache tSearchParmasCache;
    @Autowired
    public void settSearchParmasCache(TSearchParmasCache tSearchParmasCache) {
        this.tSearchParmasCache = tSearchParmasCache;
    }

    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Override
    public PageInfo<TSearchParmas> selectPage(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum, pageSize);//从缓存中进行分页查询
        List<TSearchParmas> allData = tSearchParmasCache.getAllData();
        List<TSearchParmas> collect = allData.stream()
                .skip((pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());
        //应该对查询出来的数据进行筛选，不同的用户只能看到有权限看到的信息
        PageInfo<TSearchParmas> pageInfo=new PageInfo<>(collect);
        return pageInfo;
    }

    @Override
    public void insertSearchParams(TSearchParmas tSearchParmas) {
        //判断信息添加的信息是否为空
        if (tSearchParmas.isNull(CheckType.ADD)) {
            throw new AddDataException("添加的数据不合法", ResultCode.DATA_ISNULL);
        }
        tSearchParmasMapper.insertSearchParams(tSearchParmas);

        //声明一个事件
        context.publishEvent(new TSearchParmasEvent());

    }

    @Override
    public void updateSearchParams(TSearchParmas tSearchParmas) {
        //判断信息修改的信息是否为空
        if (tSearchParmas.isNull(CheckType.UPDATE)) {
            throw new AddDataException("修改的数据不合法", ResultCode.DATA_ISNULL);
        }
        tSearchParmasMapper.updateSearchParams(tSearchParmas);

        //声明一个事件
        context.publishEvent(new TSearchParmasEvent());

    }

    @Override
    public void deleteSearchParams(int id) {
        //判断传递的主键是否合法
        if (id==0) {
            throw new AddDataException("传递的主键不合法", ResultCode.DATA_ISNULL);
        }
        tSearchParmasMapper.deleteSearchParams(id);

        //声明一个事件,进行缓存数据的更新
        context.publishEvent(new TSearchParmasEvent());
    }

    @Override
    public TSearchParmas selectByType(String type) {
        //判断传递过来的类型是否合法
        if (type==null||type==""){
            throw new QueryDataException("传递的类型不合法", ResultCode.DATA_ISNULL);
        }

        TSearchParmas byKey = tSearchParmasCache.getByKey(type);
        //看是否能从数据库中查询到数据
        if (byKey==null){
            throw new QueryDataException("没有查询到相关类型的SQL语句", ResultCode.DATA_ISNULL);
        }
        return byKey;
    }
}
